package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.FollowedShippingAddressAddRequest;
import com.frogs.ucenter.v1.request.FollowedShippingAddressDeleteRequest;
import com.frogs.ucenter.v1.request.FollowedShippingAddressListRequest;
import com.frogs.ucenter.v1.request.FollowedShippingAddressUpdateRequest;
import com.frogs.ucenter.v1.request.UserShippingAddressAddRequest;
import com.frogs.ucenter.v1.request.UserShippingAddressDefaultRequest;
import com.frogs.ucenter.v1.request.UserShippingAddressDeleteRequest;
import com.frogs.ucenter.v1.request.UserShippingAddressListRequest;
import com.frogs.ucenter.v1.request.UserShippingAddressShowDefaultRequest;
import com.frogs.ucenter.v1.request.UserShippingAddressShowRequest;
import com.frogs.ucenter.v1.request.UserShippingAddressUpdateRequest;
import com.frogs.ucenter.v1.service.UserShippingAddressService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Controller(ApiConstants.VERSION + "userShippingAddressController")
@RequestMapping("/"+ApiConstants.VERSION)
public class UserShippingAddressController extends BaseController3 {

    private static final String ROOT = "/user/address";
    private static final String FOLLOWED_ROOT = "/followed/address";
    private static final String DEFAULT = "/default";
    private static final String SHOW_DEFAULT = "/showDefault";
    
    private static final String USER_ADDRESS_LIST = ROOT + LIST;
    private static final String FOLLOWED_ADDRESS_LIST = FOLLOWED_ROOT + LIST;
    private static final String USER_ADDRESS_DETAIL = ROOT + DETAIL;
    
    private static final String USER_ADDRESS_ADD = ROOT + ADD;
    private static final String FOLLOWED_ADDRESS_ADD = FOLLOWED_ROOT + ADD;
    private static final String USER_ADDRESS_UPDATE = ROOT + UPDATE;
    private static final String FOLLOWED_ADDRESS_UPDATE = FOLLOWED_ROOT + UPDATE;
    private static final String USER_ADDRESS_DELETE = ROOT + DELETE;
    private static final String FOLLOWED_ADDRESS_DELETE = FOLLOWED_ROOT + DELETE;
    private static final String USER_ADDRESS_DEFAULT = ROOT + DEFAULT;
    private static final String USER_ADDRESS_SHOW_DEFAULT = ROOT + SHOW_DEFAULT;
    
    
    
	@Resource(name=ApiConstants.VERSION+"userShippingAddressService")
	private UserShippingAddressService userShippingAddressService;
	
    @RequestMapping(USER_ADDRESS_LIST)
    public Response userShippingAddressList(HttpServletRequest request, ModelMap model) {
        Response response = null ; 
        try{
        	UserShippingAddressListRequest listRequest = new UserShippingAddressListRequest();
        	listRequest.parse(request);
        	response = userShippingAddressService.getPageList(listRequest);
        }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
        return response;
    }
    @RequestMapping(FOLLOWED_ADDRESS_LIST)
    public Response followedShippingAddressList(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
    	try{
    		FollowedShippingAddressListRequest listRequest = new FollowedShippingAddressListRequest();
    		listRequest.parse(request);
    		response = userShippingAddressService.getFollowedPageList(listRequest);
    	}catch (Exception e) {
    		response = new FailResponse();
    		if (e instanceof AppBaseException) {
    			response.setTip(e.getMessage());
    		} else {
    			response.setTip("errro message" + e.getMessage());
    		}
    	}
    	return response;
    }
    
    @RequestMapping({USER_ADDRESS_DETAIL})
    public Response userShippingAddressEdit(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
        try{
        	UserShippingAddressShowRequest editRequest = new UserShippingAddressShowRequest();
        	editRequest.parse(request);
        	response = userShippingAddressService.getObject(editRequest);
	    }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
        return response;
    }

    @RequestMapping(USER_ADDRESS_ADD)
    public Response userShippingAddressAdd(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
        try{
	    	UserShippingAddressAddRequest addRequest = new UserShippingAddressAddRequest();
	    	addRequest.parse(request);
	    	response = userShippingAddressService.insert(addRequest);
	    }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
	    return response;
    }
    @RequestMapping(FOLLOWED_ADDRESS_ADD)
    public Response followedShippingAddressAdd(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
    	try{
    		FollowedShippingAddressAddRequest addRequest = new FollowedShippingAddressAddRequest();
    		addRequest.parse(request);
    		response = userShippingAddressService.insertFollowedAddress(addRequest);
    	}catch (Exception e) {
    		response = new FailResponse();
    		if (e instanceof AppBaseException) {
    			response.setTip(e.getMessage());
    		} else {
    			response.setTip("errro message" + e.getMessage());
    		}
    	}
    	return response;
    }

    @RequestMapping(USER_ADDRESS_UPDATE)
    public Response userShippingAddressUpdate(HttpServletRequest request, ModelMap model) throws Exception {
    	Response response = null ; 
        try{
	    	UserShippingAddressUpdateRequest updateRequest = new UserShippingAddressUpdateRequest();
	    	updateRequest.parse(request);
	    	response = userShippingAddressService.update(updateRequest);
	    }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
	    return response;
    }
    @RequestMapping(FOLLOWED_ADDRESS_UPDATE)
    public Response followedShippingAddressUpdate(HttpServletRequest request, ModelMap model) throws Exception {
    	Response response = null ; 
    	try{
    		FollowedShippingAddressUpdateRequest updateRequest = new FollowedShippingAddressUpdateRequest();
    		updateRequest.parse(request);
    		response = userShippingAddressService.updateFollowedAddress(updateRequest);
    	}catch (Exception e) {
    		response = new FailResponse();
    		if (e instanceof AppBaseException) {
    			response.setTip(e.getMessage());
    		} else {
    			response.setTip("errro message" + e.getMessage());
    		}
    	}
    	return response;
    }
    
    @RequestMapping(USER_ADDRESS_DELETE)
    public Response userShippingAddressDelete(HttpServletRequest request, ModelMap model) throws Exception {
    	Response response = null ; 
        try{
	    	UserShippingAddressDeleteRequest deleteRequest = new UserShippingAddressDeleteRequest();
	    	deleteRequest.parse(request);
	    	response = userShippingAddressService.delete(deleteRequest);
	    }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
	    return response;
    }
    @RequestMapping(FOLLOWED_ADDRESS_DELETE)
    public Response followedShippingAddressDelete(HttpServletRequest request, ModelMap model) throws Exception {
    	Response response = null ; 
    	try{
    		FollowedShippingAddressDeleteRequest deleteRequest = new FollowedShippingAddressDeleteRequest();
    		deleteRequest.parse(request);
    		response = userShippingAddressService.deleteFollowedAddress(deleteRequest);
    	}catch (Exception e) {
    		response = new FailResponse();
    		if (e instanceof AppBaseException) {
    			response.setTip(e.getMessage());
    		} else {
    			response.setTip("errro message" + e.getMessage());
    		}
    	}
    	return response;
    }
    
    @RequestMapping(USER_ADDRESS_DEFAULT)
    public Response userShippingAddressDefault(HttpServletRequest request, ModelMap model) throws Exception {
    	Response response = null ; 
        try{
        	UserShippingAddressDefaultRequest defaultRequest = new UserShippingAddressDefaultRequest();
        	defaultRequest.parse(request);
	    	response = userShippingAddressService.setDefault(defaultRequest);
	    } catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
	    return response;
    }
    
    @RequestMapping(USER_ADDRESS_SHOW_DEFAULT)
    public Response userShippingAddressShowDefault(HttpServletRequest request, ModelMap model) throws Exception {
    	Response response = null ; 
    	try{
    		UserShippingAddressShowDefaultRequest defaultRequest = new UserShippingAddressShowDefaultRequest();
    		defaultRequest.parse(request);
    		response = userShippingAddressService.showDefault(defaultRequest);
    	} catch (Exception e) {
    		response = new FailResponse();
    		if (e instanceof AppBaseException) {
    			response.setTip(e.getMessage());
    		} else {
    			response.setTip("errro message" + e.getMessage());
    		}
    	}
    	return response;
    }
}